package main;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class GUI extends JFrame {

	private static final long serialVersionUID = 2980934173880666285L;

	private static GUI instance = null;
	
	//
	private boolean isPlaying = false;
	
	private int HUE_MIN = 0;
	private int HUE_MAX = 256;
	
	private int SAT_MIN = 0;
	private int SAT_MAX = 256;
	
	private int VAL_MIN = 0;
	private int VAL_MAX = 256;
	
	//sliders
	private JSlider hMin;
	private JSlider hMax;
	
	private JSlider sMin;
	private JSlider sMax;
	
	private JSlider vMin;
	private JSlider vMax;
	
	//numericke vrednosti
	private JLabel lblHMinR;
	private JLabel lblHMaxR;
	private JLabel lblSMinR;
	private JLabel lblSMaxR;
	private JLabel lblVMinR;
	private JLabel lblVMaxR;
	
	private GUI() {
		init();
	}
	
	private void init() {
		setSize(new Dimension(600, 230));
		setTitle("Color range finder");
		
		Box mainBox = new Box(BoxLayout.Y_AXIS);
		
		//hMin slajder
		JPanel hMinPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		hMin = new JSlider(0, 256, HUE_MIN);
		hMin.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider)e.getSource();
				
				if(!source.getValueIsAdjusting()) {
					HUE_MIN = source.getValue();
					lblHMinR.setText(String.valueOf(HUE_MIN));
				}
				
			}
		});
		
		JLabel lblHMin = new JLabel("HUE MIN");
		lblHMinR = new JLabel(String.valueOf(HUE_MIN));
		hMinPanel.add(lblHMin);
		hMinPanel.add(hMin);
		hMinPanel.add(lblHMinR);
		mainBox.add(hMinPanel);
		
		//hMax
		JPanel hMaxPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		hMax = new JSlider(0, 256, HUE_MAX);
		hMax.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider)e.getSource();
				
				if(!source.getValueIsAdjusting()) {
					HUE_MAX = source.getValue();
					lblHMaxR.setText(String.valueOf(HUE_MAX));
				}
				
			}
		});

		JLabel lblHMax = new JLabel("HUE MAX");
		lblHMaxR = new JLabel(String.valueOf(HUE_MAX));
		hMaxPanel.add(lblHMax);
		hMaxPanel.add(hMax);
		hMaxPanel.add(lblHMaxR);
		mainBox.add(hMaxPanel);
		
		//sMin
		JPanel sMinPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		sMin = new JSlider(0, 256, SAT_MIN);
		sMin.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider)e.getSource();
				
				if(!source.getValueIsAdjusting()) {
					SAT_MIN = source.getValue();
					lblSMinR.setText(String.valueOf(SAT_MIN));
				}
				
			}
		});
		JLabel lblSMin = new JLabel("SAT MIN");
		lblSMinR = new JLabel(String.valueOf(SAT_MIN));
		sMinPanel.add(lblSMin);
		sMinPanel.add(sMin);
		sMinPanel.add(lblSMinR);
		mainBox.add(sMinPanel);
		
		//sMax
		JPanel sMaxPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		sMax = new JSlider(0, 256, SAT_MAX);
		sMax.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider)e.getSource();
				
				if(!source.getValueIsAdjusting()) {
					SAT_MAX = source.getValue();
					lblSMaxR.setText(String.valueOf(SAT_MAX));
				}
				
			}
		});
		JLabel lblSMax = new JLabel("SAT MAX");
		lblSMaxR = new JLabel(String.valueOf(SAT_MAX));
		sMaxPanel.add(lblSMax);
		sMaxPanel.add(sMax);
		sMaxPanel.add(lblSMaxR);
		mainBox.add(sMaxPanel);
		
		//vMin
		JPanel vMinPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		vMin = new JSlider(0, 256, VAL_MIN);
		vMin.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider)e.getSource();
				
				if(!source.getValueIsAdjusting()) {
					VAL_MIN = source.getValue();
					lblVMinR.setText(String.valueOf(VAL_MIN));
				}
				
			}
		});
		JLabel lblVMin = new JLabel("VAL MIN");
		lblVMinR = new JLabel(String.valueOf(VAL_MIN));
		vMinPanel.add(lblVMin);
		vMinPanel.add(vMin);
		vMinPanel.add(lblVMinR);
		mainBox.add(vMinPanel);
		
		//vMax
		JPanel vMaxPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		vMax = new JSlider(0, 256, VAL_MAX);
		vMax.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider)e.getSource();
				
				if(!source.getValueIsAdjusting()) {
					VAL_MAX = source.getValue();
					lblVMaxR.setText(String.valueOf(VAL_MAX));
				}
				
			}
		});
		JLabel lblVMax = new JLabel("VAL MAX");
		lblVMaxR = new JLabel(String.valueOf(VAL_MAX));
		vMaxPanel.add(lblVMax);
		vMaxPanel.add(vMax);
		vMaxPanel.add(lblVMaxR);
		mainBox.add(vMaxPanel);
		
		//PLAY BUTTON
		JPanel playPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton btnPlay = new JButton("Play");
		btnPlay.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setPlaying(true);
				System.out.println(isPlaying);
				
			}
		});
		playPanel.add(btnPlay);
		mainBox.add(playPanel);

		JPanel container = new JPanel();
		container.add(mainBox);
		
		add(container);
		setVisible(true);
		
	}
	
	public static GUI getInstance() {
		if(instance == null)
			instance = new GUI();
		
		return instance;
	}
	
	public int getHUE_MIN() {
		return HUE_MIN;
	}
	
	public int getHUE_MAX() {
		return HUE_MAX;
	}
	
	public int getSAT_MIN() {
		return SAT_MIN;
	}
	
	public int getSAT_MAX() {
		return SAT_MAX;
	}
	
	public int getVAL_MIN() {
		return VAL_MIN;
	}
	
	public int getVAL_MAX() {
		return VAL_MAX;
	}
	
	public void setPlaying(boolean isPlaying) {
		this.isPlaying = isPlaying;
	}
	
	public boolean isPlaying() {
		return isPlaying;
	}
	
	public void setLblHMinR(JLabel lblHMinR) {
		this.lblHMinR = lblHMinR;
	}
	
	public void setLblHMaxR(JLabel lblHMaxR) {
		this.lblHMaxR = lblHMaxR;
	}
	
	public void setLblSMaxR(JLabel lblSMaxR) {
		this.lblSMaxR = lblSMaxR;
	}
	
	public void setLblSMinR(JLabel lblSMinR) {
		this.lblSMinR = lblSMinR;
	}
	
	public void setLblVMaxR(JLabel lblVMaxR) {
		this.lblVMaxR = lblVMaxR;
	}
	
	public void setLblVMinR(JLabel lblVMinR) {
		this.lblVMinR = lblVMinR;
	}
	
}

